import { Column, Entity, JoinColumn, ManyToOne } from 'typeorm';
import { BaseEntity } from '../common/base/base.entity';
import { Order } from './order.entity';
import { Product } from './product.entity';

@Entity('order_items')
export class OrderItem extends BaseEntity {
  @Column({
    type: 'bigint',
    comment: '订单ID',
  })
  orderId: string;

  @Column({
    type: 'bigint',
    nullable: true,
    comment: '商品ID',
  })
  productId?: string;

  @Column({
    type: 'varchar',
    length: 200,
    nullable: true,
    comment: '商品名称快照',
  })
  productName?: string;

  @Column({
    type: 'varchar',
    length: 500,
    nullable: true,
    comment: '商品封面快照',
  })
  productCover?: string;

  @Column({
    type: 'decimal',
    precision: 10,
    scale: 2,
    nullable: true,
    comment: '原价',
  })
  originalPrice?: number;

  @Column({
    type: 'decimal',
    precision: 10,
    scale: 2,
    nullable: true,
    comment: '成交价',
  })
  actualPrice?: number;

  @Column({
    type: 'int',
    default: 1,
    comment: '数量',
  })
  quantity: number;

  @Column({
    type: 'enum',
    enum: ['vip', 'svip'],
    nullable: true,
    comment: '会员等级',
  })
  membershipLevel?: 'vip' | 'svip';

  @Column({
    type: 'int',
    nullable: true,
    comment: '会员月数',
  })
  membershipMonths?: number;

  // 关联
  @ManyToOne(() => Order, (order) => order.items)
  @JoinColumn({ name: 'orderId' })
  order: Order;

  @ManyToOne(() => Product)
  @JoinColumn({ name: 'productId' })
  product?: Product;
}
